# 1.读取my_directory/data.json文件中的data部分
# 2.将用户的id作为键，将username、sex、city、sing一起作为值，保存为一个user_data.json文件

import json

# encoding表示用指定编码格式读取
with open("my_directory/data.json", "r", encoding="utf-8") as file:
    data = json.load(file)['data']

# 保存自定义字典
user_map = {}
# 遍历列表中的数据
for user in data:
    # id当作键
    key = user['id']
    # 构造自定义的值
    value = {
        "username": user['username'],
        "sex": user['sex'],
        "city": user['city'],
        "sign": user['sign'],
    }
    # 添加每个构造好的键值对，保存到新的字典中
    user_map[key] = value

# 写入
with open("my_directory/user_map.json", "w", encoding="utf-8") as file:
    json.dump(user_map, file)
print("写入成功")
